package cn.gov.pbc.gd.credit.log.service.inquiry.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liu.jbase.PageListVo;
import com.liu.jbase.ResultVo;
import com.liu.jbase.constant.ResCode;
import com.liu.tools.DateUtil;
import com.liu.tools.LogUtil;

import cn.gov.pbc.gd.credit.api.log.ILogInquiryApi;
import cn.gov.pbc.gd.credit.common.constant.CodeConstant;
import cn.gov.pbc.gd.credit.common.constant.CommonConstant;
import cn.gov.pbc.gd.credit.common.model.UserModel;
import cn.gov.pbc.gd.credit.common.service.redis.IRedisService;
import cn.gov.pbc.gd.credit.common.util.UserUtil;
import cn.gov.pbc.gd.credit.log.service.inquiry.ILogInquiryService;
import cn.gov.pbc.gd.credit.vo.log.LogInquiryVo;


@Service
public class LogInquiryServiceImpl implements ILogInquiryService{

	private static Logger logger = LoggerFactory.getLogger(LogInquiryServiceImpl.class);
	
	@Autowired
	private ILogInquiryApi logInquiryApi;
	
	@Autowired
	private IRedisService redisService;
	
	@Override
	public ResultVo<PageListVo<LogInquiryVo>> page(LogInquiryVo vo, HttpServletRequest request) {
		
		//Query query = new Query(Criteria.where("operatorId").is(vo.getInquiryIp()).and("cname").is("zcy1"));
		
        UserModel userModel = UserUtil.getUserModel(vo.getSid(), redisService);
		
		boolean flag = false;

		if(UserUtil.checkRoleType(userModel.getRoleList(), CommonConstant.ROLE_BUSI_ADMIN)){//业务管理员
			flag = true;
		}
		
		if(! flag && UserUtil.checkRoleType(userModel.getRoleList(), CommonConstant.ROLE_OPRTATOR)){//查询员
			vo.setAccount(userModel.getAccount());
		}
		
		PageListVo<LogInquiryVo> pageListVo = new PageListVo<>();
		pageListVo.setCurrPage(vo.getCurPage());
		pageListVo.setPageSize(vo.getPageSize());

		ResultVo<PageListVo<LogInquiryVo>> resultVo = new ResultVo<>();
		resultVo.setCode(CodeConstant.SUCCESS);
		
		try {
			PageListVo<LogInquiryVo> pageList = logInquiryApi.getPage(vo, pageListVo);
			resultVo.setData(pageList);
		} catch (Exception e) {
			logger.error(LogUtil.msg("LogInquiryServiceImpl:page", "查询登录日志分页异常"), e);
			resultVo.setCode(CodeConstant.EXCEPTION);			
		}
		
		return resultVo;
	}

	@Override
	public ResultVo<List<LogInquiryVo>> exportExcel(LogInquiryVo vo, HttpServletRequest request, HttpServletResponse response) {
		
        UserModel userModel = UserUtil.getUserModel(vo.getSid(), redisService);
		
		boolean flag = false;

		if(UserUtil.checkRoleType(userModel.getRoleList(), CommonConstant.ROLE_BUSI_ADMIN)){//业务管理员
			flag = true;
		}
		
		if(! flag && UserUtil.checkRoleType(userModel.getRoleList(), CommonConstant.ROLE_OPRTATOR)){//查询员
			vo.setAccount(userModel.getAccount());
		}
		
		ResultVo<List<LogInquiryVo>> resultVo = new ResultVo<>();
		resultVo.setCode(ResCode.SUCCESS);
		
		List<LogInquiryVo> list = new ArrayList<>();
		
		try {
			list = logInquiryApi.getAll(vo);
		} catch (Exception e1) {
			
			logger.error(LogUtil.msg("LogInquiryServiceImpl:exportExcel", "查询征信查询日志异常"), e1);
			return resultVo;
		}
		
		Map<String, Object> map = new HashMap<>();
		
		map.put("curTime", DateUtil.format(new Date(), DateUtil.FORMAT_DATE));
		map.put("beginTime", vo.getBeginTimeStr());
		if(vo.getBeginTime() == null){
			map.put("beginTime", "2017-06-15");
		}
		map.put("endTime", vo.getEndTimeStr());
		if(vo.getEndTime() == null){
			map.put("endTime", DateUtil.format(new Date(), DateUtil.FORMAT_DATE));
		}
		
		map.put("totalList", list);

		//String excelPath = ConfigurerEx.getProperty("report.query.list.template");
		
//		ReportEnginer enginer = new ReportEnginer();
//		
//		InputStream in = null;
//		
//		try {
//			
//			String fileName = "inquery_" + DateUtil.getCurrentFormatMill() + ".xls";
//			
//			String mimeType=  "application/octet-stream; charset=UTF-8";
//			response.setCharacterEncoding("UTF-8");  
//	        response.setContentType(mimeType);
//	        response.setHeader("Content-Disposition", String.format("inline; filename=\"" + fileName +"\""));
//			
//			OutputStream out = response.getOutputStream();	
//			in = new FileInputStream(excelPath);
//			enginer.excute(in, map, out);
//			
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		
//		if(in != null){
//			try {
//				in.close();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
		
		return resultVo;

	}

	
}
